'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    let res= await this.app.mysql.get("blog_content",{})
    ctx.body = {data:res}; 
  }

  async getArticleList(){
    let sql=`SELECT a.id,a.title,
    a.article_content
    ,a.introduce,DATE_FORMAT(a.create_time,'%Y-%m-%d' ) as create_time
    ,a.view_count,type.type_name
     FROM article as a left join type ON a.type_id = type.id`;
     let res= await this.app.mysql.query(sql)
     this.ctx.body = {data:res};
  }

  async getDetail(){
    let id =this.ctx.request.query.id;

    let sql = `
    SELECT a.id,a.title,
    a.article_content
    ,a.introduce,DATE_FORMAT(a.create_time,'%Y-%m-%d' ) as create_time
    ,a.view_count,type.type_name,
    type.id as type_id
     FROM article as a left join type ON a.type_id = type.id
		 where a.id= ${id}
    `
    let res = await this.app.mysql.query(sql)
    let updatesql =`update article set view_count=${res[0].view_count+1} where article.id=${id}`
    this.app.mysql.query(updatesql)
    this.ctx.body={data:res}
  }
} 

module.exports = HomeController;
